﻿using System;
using System.Windows.Data;

namespace HobSoft.SoccerBet2014.Common.Wpf.Converter
{
    public class NumericCompareConverter : IMultiValueConverter
    {
        public object True { get; set; }
        public object False { get; set; }
        public object Default { get; set; }

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (True == null)
                throw new ArgumentNullException("True", "need True result");
            if (False == null)
                throw new ArgumentNullException("False", "need False result");
            if (Default == null)
                throw new ArgumentNullException("Default", "need Default result");

            if (values.Length < 2) return false;
            var type1 = values[0].GetType();
            var type2 = values[1].GetType();
            if (type1.IsValueType && type2.IsValueType)
            {
                try
                {
                    var num1 = System.Convert.ToDecimal(values[0]);
                    var num2 = System.Convert.ToDecimal(values[1]);
                    bool result = false;
                    var compareSign = (parameter == null) ? "=" : parameter.ToString();
                    switch (compareSign)
                    {
                        case ">":
                            result = num1 > num2;
                            break;
                        case "<":
                            result = num1 < num2;
                            break;
                        case ">=":
                            result = num1 >= num2;
                            break;
                        case "<=":
                            result = num1 <= num2;
                            break;
                        default:
                            result = num1.Equals(num2);
                            break;
                    }
                    return result ? True : False;
                }
                catch
                {
                    return Default;
                }
            }
            return Default;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

    }
}
